const dotenv = require('dotenv')
const path = require('path')
const resolveApp = relativePath => path.resolve(process.cwd(), relativePath)
let mode = 'development'
let i = process.argv.lastIndexOf('--mode')
if (i !== -1) {
  mode = process.argv[i + 1]
}

dotenv.config({
  path: resolveApp(`.env.${mode}`)
})
if (!process.env.NODE_ENV && /^development|test|production$/.test(mode)) {
  process.env.NODE_ENV = mode
}

dotenv.config({
  path: resolveApp('.env')
})

module.exports = dotenv
